/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fanta.framework.dao;
import fanta.framework.manager.ConnectionManager;
import fanta.framework.persistence.Modulo;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
/**
 *
 * @author Raizer
 */
public class ModuloDAO {
private static final Logger logger = Logger.getLogger(ModuloDAO.class);
private static Modulo creareModulo ( int numeroD, int numeroC, int numeroA, ConnectionManager cm){
       Modulo ut = new Modulo();
       EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            ut.setNumeroD(numeroD);
            ut.setNumeroC(numeroC);
            ut.setNumeroA(numeroA);
            entityManager.persist(ut);
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            logger.info("Nuovo Modulo:"+ut);

        } catch (Exception e) {
            logger.error("Errore in ModuloDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return ut;

}
public static boolean aggiornaModulo(Modulo modulo, ConnectionManager cm) {
        boolean result = false;
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.persist(entityManager.merge(modulo));
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in aggiornaModuloDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return result;
    }
public static boolean cancellaModulo(Integer id, ConnectionManager cm) {
        boolean result = false;
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try{
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.remove(entityManager.find(Modulo.class, id));
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in cancellaModuloDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }

        return result;
    }
public static Modulo cercaModuloById(Integer id, ConnectionManager cm) {
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Modulo ut = new Modulo();
        try {
            Object obj = entityManager.find(Modulo.class, id);
            if(obj != null) {
                ut = (Modulo)obj;
            }
        }catch (Exception e) {
            logger.error("Errore in CercaModuloDAO", e);
        }
        return ut;
   }
}
